package com.sec.android.app.myfiles.module.local.category;

import android.content.Context;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.feature.PrivateModeMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.module.abstraction.AbsMenuImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.module.shortcut.ShortcutUtils;
import com.sec.android.app.myfiles.navigation.NavigationManager;
import com.sec.android.app.myfiles.util.StorageMonitor;
import com.sec.android.app.myfiles.util.UiUtils;

import java.util.ArrayList;

public class CategoryMenuImp extends AbsMenuImp {

    public CategoryMenuImp(Context context, StorageType supportStorage) {
        super(context, supportStorage);
    }

    @Override
    public void onPrepareNormalOptionsMenu(Menu menu) {
        super.onPrepareNormalOptionsMenu(menu);

        setMenuItemVisibility(menu, R.id.menu_clear_recent_history, false);
        setMenuItemVisibility(menu, R.id.menu_clear_history, false);
        setMenuItemVisibility(menu, R.id.menu_create_folder, false);
        setMenuItemVisibility(menu, R.id.menu_show_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_hide_hidden_files, false);
        setMenuItemVisibility(menu, R.id.menu_manage_shortcut, AppFeatures.isTabletUIMode() && NavigationManager.isPossibleToActionMode() && ShortcutUtils.getShortcutItemsCount(mContext) > 0);

        if (UiUtils.isCategoryFolderList(mNavigationInfo.getCurRecord())) {
            setMenuItemVisibility(menu, R.id.menu_sort_by, false);
        }
    }

    @Override
    public void onPrepareEditOptionsMenu(Menu menu) {
        super.onPrepareEditOptionsMenu(menu);

        int selectedCount = mFragment.getSelectedFileCount();
        boolean isCategoryFolderList = UiUtils.isCategoryFolderList(mNavigationInfo.getCurRecord());

        if (selectedCount > 0) {
            ArrayList<FileRecord> selectedItem = mFragment.getSelectedFile();
            if (isSharableRecords(selectedItem)) {
                setMenuItemVisibility(menu, R.id.menu_share, true);
            }

            if (AppFeatures.isTabletUIMode() && mFragment.getActionModeType() == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION) {
                showMoreMenu(menu, R.id.menu_move, false);
            }

            if (isCategoryFolderList) {
                setMenuItemVisibility(menu, R.id.menu_copy, false);
                setMenuItemVisibility(menu, R.id.menu_move, false);
            }

            PrivateModeMgr privateModeMgr = PrivateModeMgr.getInstance(mContext);
            PrivatePath pathSts = PrivatePath.ONLY_NON_PRIVATE;
            if (privateModeMgr.isReady() && !isCategoryFolderList) {
                pathSts = getSelectedItemPrivatePathStatus(selectedItem);
                setMenuItemVisibility(menu, R.id.menu_move_to_private, (pathSts != PrivatePath.ONLY_PRIVATE));
                setMenuItemVisibility(menu, R.id.menu_remove_from_private, (pathSts == PrivatePath.ONLY_PRIVATE));
            }

            showKnoxMenu(menu, false, pathSts);

            if (selectedCount == 1 && selectedItem != null && !selectedItem.isEmpty()) {
                FileRecord item = selectedItem.get(0);
                if (item != null) {
                    if (!isCategoryFolderList && ShortcutUtils.isSupportShortcut(mContext, item)) {
                        setMenuItemVisibility(menu, R.id.menu_add_shortcut, true);
                    }
                }
                setMenuItemVisibility(menu, R.id.menu_rename, isRenamePossible(item));
            }
        }
    }

    private boolean isRenamePossible(FileRecord record) {
        boolean bRet = false;
        if (record != null && !(StorageMonitor.isStorageRoot(record.getFullPath())) && !UiUtils.isCategoryFolderList(mNavigationInfo.getCurRecord())) {
            bRet = true;
        }
        return bRet;
    }

    @Override
    protected void createContextualMenuOnBackground(ContextMenu menu) {
        super.createContextualMenuOnBackground(menu);
        if (UiUtils.isCategoryFolderList(mNavigationInfo.getCurRecord())) {
            menu.removeItem(R.id.menu_share);
            menu.removeItem(R.id.menu_sort_by);
            menu.removeItem(R.id.menu_show_hidden_files);
            menu.removeItem(R.id.menu_hide_hidden_files);
        }
        menu.removeItem(R.id.menu_create_folder);
        menu.removeItem(R.id.menu_paste);
    }

    @Override
    protected void _createContextualMenuOnItems(ContextMenu menu, FileRecord record, boolean isForOneItem) {
        super._createContextualMenuOnItems(menu, record, isForOneItem);
        if (UiUtils.isCategoryFolderList(mNavigationInfo.getCurRecord())) {
            menu.removeItem(R.id.menu_move);
            menu.removeItem(R.id.menu_copy);
            menu.removeItem(R.id.menu_rename);
            menu.removeItem(R.id.menu_add_shortcut);
            menu.removeItem(R.id.menu_share);
        }
        menu.removeItem(R.id.menu_paste);
        menu.removeItem(R.id.menu_zip);
        menu.removeItem(R.id.menu_unzip);
        menu.removeItem(R.id.menu_unzip_current_folder);
    }

    @Override
    public boolean _onContextualItemSelected(MenuItem item, ArrayList<FileRecord> srcRecordList, FileRecord record, AbsMyFilesFragment fragment) {
        if (fragment != null) {
            srcRecordList = new ArrayList<>();
            AbsMyFilesFragment.ActionModeType actionModeType = fragment.getActionModeType();
            if (fragment.getTouchPosition() == BACKGROUND) {
                return super._onContextualItemSelected(item, null, fragment.getNavigationInfo().getCurRecord(), fragment);
            } else {
                if (actionModeType == AbsMyFilesFragment.ActionModeType.NORMAL) {
                    srcRecordList.add(record);
                    return super._onContextualItemSelected(item, srcRecordList, record, fragment);
                } else if (actionModeType == AbsMyFilesFragment.ActionModeType.EDIT_FILE_FOLDER_OPERATION || actionModeType == AbsMyFilesFragment.ActionModeType.SELECT_FILE_FOLDER_OPERATION) {
                    srcRecordList = fragment.getSelectedFile();
                    if (srcRecordList != null && !srcRecordList.isEmpty()) {
                        FileRecord recordList = srcRecordList.get(0);
                        return super._onContextualItemSelected(item, srcRecordList, recordList, fragment);
                    }
                }
            }
        }
        return false;
    }

    @Override
    protected StorageType getTargetStorageType() {
        return StorageType.Category;
    }
}
